<template>
	<ynet-content id="branchMain" :headerInformation="true" :headerMenu="true">
		<div class="main-container"><!--提示当前位置-->
			<ynet-breadcrumb></ynet-breadcrumb>
		</div>
		
		<div class="main-container mt20"  v-if="tree">
			<ynet-branchTree :roleBsnList="roleBsnList" @modifyMenuTree="modifyMenuTree" @Detail="Detail"></ynet-branchTree>
			<div class="btnArea mt20 tac">
				<el-button type="primary" icon="search" @click="branchAdd()" >Add</el-button>
				<el-button type="primary" icon="edit" @click="branchEdit()" >Modify</el-button>
				<!--<el-button type="primary" icon="edit" @click="deleteBranch()" >删除</el-button>-->
			</div>
		</div>
		<!--机构新增 -->
		<div v-if="isAdd">
			<BranchAdd :formDataAdd="formDataAdd" @back="backMenu"></BranchAdd>
		</div>
		<div v-if="isEdit">
			<BranchModify :formData="formData" @back="backMenu"></BranchModify>
		</div>
		<div v-if="isDetail">
			<BranchDetail :formData="formData" @back="backMenu"></BranchDetail>
		</div>
	</ynet-content>
</template>

<script>
import BranchAdd from "./components/branchAdd";
import BranchModify from "./components/branchModify";
import BranchDetail from "./components/branchDetail";
export default {
  name: "BrachMain",
  components: {
    BranchAdd,
    BranchModify,
    BranchDetail
  },
  data() {
    return {
      tree: true,
      disabled: true,
      roleBsnList: [],
      tableData1: [],
      branchInfoList: [], //从数据库查询到所有的机构信息列表
      isDetail: false,
      main: true,
      isAdd: false,
      isEdit: false,
      formData: {
        //机构详情页数据,机构修改页接收的数据
      },
      selectData: [], //页面选中的数据
      formDataEdit: {
        //修改参数过度数据
        brhParentName: ""
      },
      formDataAdd: {
        //新增参数过度数据
        brhParentName: "",
        brhParentId: "",
        brhLevel: ""
      }
    };
  },
  methods: {
    branchAdd() {
      if (this.selectData.length != 1) {
        this.$alert("Selected only one");
      } else {
        this.formDataAdd.brhParentId = this.selectData[0].brhId;
        this.formDataAdd.brhParentName = this.selectData[0].brhName;
        if (this.selectData[0].brhLevel == 1) {
          this.formDataAdd.brhLevel = 2;
          this.isAdd = true;
          this.tree = false;
        } else if (this.selectData[0].brhLevel == 2) {
          this.formDataAdd.brhLevel = 3;
          this.isAdd = true;
          this.tree = false;
        } else {
          this.$alert("subbranch cannot add branch", "warning", {
            confirmButtonText: "Confirm",
            callback: action => {}
          });
        }
      }
    },

    branchEdit() {
      if (this.selectData.length != 1) {
        this.$alert("Selected only one", "warning", {
          confirmButtonText: "Confirm",
          callback: action => {}
        });
      } else {
        this.formData = this.selectData[0];
        for (let i = 0; i < this.branchInfoList.length; i++) {
          if (this.selectData[0].brhParentId == this.branchInfoList[i].brhId) {
            this.formDataEdit.brhParentName = this.branchInfoList[i].brhName;
            this.formData = $.extend({}, this.formData, this.formDataEdit);
            break;
          }
        }
        this.isEdit = true;
        this.tree = false;
      }
    },
    backMenu(val) {
      this.selectData = [];
      this.tableData1 = [];
      this.branchInfoList = [];
      this.roleBsnList = [];
      this.tree = true;
      this.isDetail = false;
      this.isAdd = false;
      this.isEdit = false;
      this.freshPage();
    },
    handleChange(selection, row) {
      this.selectData = selection;
    },

    openDetail(index, row) {
      for (let i = 0; i < this.branchInfoList.length; i++) {
        if (this.branchInfoList[i].brhId == row) {
          this.formData = this.branchInfoList[i];
          this.isDetail = true;
          this.main = false;
          break;
        }
      }
    },
    modifyMenuTree(val) {
      console.log("val:" + JSON.stringify(val));
      this.selectData = [];
      if (val.length > 0) {
        this.disabled = false;
      } else {
        this.disabled = true;
      }
      for (let i = 0; i < val.length; i++) {
        let brhId = val[i].brhId;
        for (let j = 0; j < this.branchInfoList.length; j++) {
          if (this.branchInfoList[j].brhId == brhId) {
            this.selectData.push(this.branchInfoList[j]);
          }
        }
      }
    },
    Detail(val) {
      this.formData = val;
      this.isDetail = true;
      this.tree = false;
    },
    freshPage() {
      this.$tools.request(this, "IM010101Action.do").then(data => {
        //alert(JSON.stringify(data.body.branchInfoList))
        if (data.body.errorCode == 0) {
          this.branchInfoList = data.body.branchInfoList;
          for (let i = 0; i < this.branchInfoList.length; i++) {
            if (this.branchInfoList[i].brhLevel == "1") {
              this.tableData1.push(this.branchInfoList[i]);
            }
          }
          console.log(this.tableData1);
          for (let j = 0; j < this.tableData1.length; j++) {
            let brhId = this.tableData1[j].brhId;
            let childrens = [];
            for (let k = 0; k < this.branchInfoList.length; k++) {
              if (brhId == this.branchInfoList[k].brhParentId) {
                childrens.push(this.branchInfoList[k]);
                this.tableData1[j].childrens = childrens;
              }
            }
          }
          for (let l = 0; l < this.tableData1.length; l++) {
            for (let x = 0; x < this.tableData1[l].childrens.length; x++) {
              let brhId1 = this.tableData1[l].childrens[x].brhId;
              let childrens1 = [];
              for (let y = 0; y < this.branchInfoList.length; y++) {
                if (brhId1 == this.branchInfoList[y].brhParentId) {
                  childrens1.push(this.branchInfoList[y]);
                  this.tableData1[l].childrens[x].childrens = childrens1;
                }
              }
            }
          }
          //console.log(this.tableData1)
          this.roleBsnList = this.tableData1;
        } else {
          this.$alert(data.body.errorMsg, "warning", "waring", {
            confirmButtonText: "Confirm",
            callback: action => {}
          });
        }
      });
    }
  },
  mounted() {
    this.freshPage();
    //console.log("menu:"+JSON.stringify(this.$tools.storage.get("menu", "save")))
  }
};
</script>